//
//  UIView+Trait.m
//  demo
//
//  Created by Jarvis on 2024/5/31.
//

#import "UIView+Trait.h"
#import <objc/runtime.h>
#import "Aspects.h"


@interface UIResponder (TraitBlock)
@property (nonatomic, weak) id<UITraitChangeRegistration> traitChangeRegistration API_AVAILABLE(ios(17.0));
@end
@implementation UIResponder (TraitBlock)
- (id<UITraitChangeRegistration>)traitChangeRegistration  API_AVAILABLE(ios(17.0)){
    return objc_getAssociatedObject(self, _cmd);
}
- (void)setTraitChangeRegistration:(id<UITraitChangeRegistration>)traitChangeRegistration  API_AVAILABLE(ios(17.0)){
    objc_setAssociatedObject(self, @selector(traitChangeRegistration), traitChangeRegistration, OBJC_ASSOCIATION_ASSIGN);
}
@end



@implementation UIView (Trait)

- (void)observeTraitChanges:(void(^)(UIUserInterfaceStyle style))traitChangesBlock {
    if (@available(iOS 17.0, *)) {
        self.traitChangeRegistration = [self registerForTraitChanges:@[UITraitUserInterfaceStyle.class] withHandler:^(__kindof id<UITraitEnvironment>  _Nonnull traitEnvironment, UITraitCollection * _Nonnull previousCollection) {
            UIUserInterfaceStyle style = UITraitCollection.currentTraitCollection.userInterfaceStyle;
            !traitChangesBlock ?: traitChangesBlock(style);
        }];
    } else {
        [self aspect_hookSelector:@selector(traitCollectionDidChange:) withOptions:AspectPositionAfter usingBlock:^{
            if (@available(iOS 13.0, *)) {
                UIUserInterfaceStyle style = UITraitCollection.currentTraitCollection.userInterfaceStyle;
                !traitChangesBlock ?: traitChangesBlock(style);
            } else {
                UIUserInterfaceStyle style = self.traitCollection.userInterfaceStyle;
                !traitChangesBlock ?: traitChangesBlock(style);
            }
        } error:nil];
    }
}
- (void)removeTraitObserver {
    if (@available(iOS 17.0, *)) {
        [self unregisterForTraitChanges:self.traitChangeRegistration];
    }
}

@end

@implementation UIViewController (Trait)

- (void)observeTraitChanges:(void(^)(UIUserInterfaceStyle style))traitChangesBlock {
    if (@available(iOS 17.0, *)) {
        self.traitChangeRegistration = [self registerForTraitChanges:@[UITraitUserInterfaceStyle.class] withHandler:^(__kindof id<UITraitEnvironment>  _Nonnull traitEnvironment, UITraitCollection * _Nonnull previousCollection) {
            UIUserInterfaceStyle style = UITraitCollection.currentTraitCollection.userInterfaceStyle;
            !traitChangesBlock ?: traitChangesBlock(style);
        }];
    } else {
        [self aspect_hookSelector:@selector(traitCollectionDidChange:) withOptions:AspectPositionAfter usingBlock:^{
            if (@available(iOS 13.0, *)) {
                UIUserInterfaceStyle style = UITraitCollection.currentTraitCollection.userInterfaceStyle;
                !traitChangesBlock ?: traitChangesBlock(style);
            } else {
                UIUserInterfaceStyle style = self.traitCollection.userInterfaceStyle;
                !traitChangesBlock ?: traitChangesBlock(style);
            }
        } error:nil];
    }
}
- (void)removeTraitObserver {
    if (@available(iOS 17.0, *)) {
        [self unregisterForTraitChanges:self.traitChangeRegistration];
    }
}

@end
